package com.baidu.shoppingmall.comment.service.impl;

import com.baidu.shoppingmall.comment.constant.CommentPictureUploadDirType;
import com.baidu.shoppingmall.comment.dao.CommentPictureDAO;
import com.baidu.shoppingmall.comment.domain.CommentPicturePO;
import com.baidu.shoppingmall.comment.service.CommentPictureService;
import com.baidu.shoppingmall.common.util.FileDfsUtil;
import com.baidu.shoppingmall.common.util.JudgePictureUploadUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.Objects;


/**
 * 评论晒图管理模块的service
 * @author yjf
 *
 */
@Service
public class CommentPictureServiceImpl extends ServiceImpl<CommentPictureDAO,CommentPicturePO> implements CommentPictureService {

	private static final Logger logger = LoggerFactory.getLogger(CommentPictureServiceImpl.class);

	/**
	 * 评论晒图管理模块的DAO
	 */
	@Autowired
	private CommentPictureDAO commentPictureDAO;

	/**
	 * 评论晒图的上传目录的类型：relative是相对路径，absolute是绝对路径
	 */
	@Value("${comment.picture.upload.dir.type}")
	private String uploadDirType;

	/**
	 * 评论晒图的上传目录
	 */
	@Value("${comment.picture.upload.dir}")
	private String uploadDirPath;


	@Autowired
	private FileDfsUtil fileDfsUtil;

	@Autowired
	private JudgePictureUploadUtils judgePictureUploadUtils;

	/**
	 * 保存评论晒图
	 * @param commentInfoId 评论信息id
	 * @param files 评论晒图
	 * @return 处理结果
	 */
	@Override
    public Boolean saveCommentPictures(Long commentInfoId, MultipartFile[] files) {

		String result = judgePictureUploadUtils.judgePictureUpload(files);
		if (Objects.equals(result,"")){
			return false;
		}
		// 将评论晒图信息保存到数据库中去
		try {
			CommentPicturePO commentPicturePO = new CommentPicturePO();
			commentPicturePO.setCommentInfoId(commentInfoId);
			commentPicturePO.setCommentPicturePath(result);
			commentPicturePO.setCreateTime(new Date());
			commentPicturePO.setUpdateTime(new Date());
			commentPictureDAO.insert(commentPicturePO);
		} catch (Exception e) {
			throw new RuntimeException("图片添加失败");
			//return false;
		}
		return true;
	}
}
